package com.tiocloud.chat.mvp.upload;

import android.app.Activity;

import com.lzy.okgo.model.Progress;
import com.lzy.okgo.request.base.Request;
import com.watayouxiang.androidutils.tools.TioLogger;
import com.watayouxiang.androidutils.widget.TioToast;
import com.watayouxiang.androidutils.widget.dialog.progress.SingletonProgressDialog;
import com.watayouxiang.httpclient.TioHttpClient;
import com.watayouxiang.httpclient.callback.TioCallback;
import com.watayouxiang.httpclient.model.BaseResp;
import com.watayouxiang.httpclient.model.request.UploadFileReq;
import com.watayouxiang.httpclient.model.request.UploadImgReq;
import com.watayouxiang.httpclient.model.request.UploadVideoReq;

/**
 * author : TaoWang
 * date : 2020/3/9
 * desc :
 */
public class UploadPresenter extends UploadContract.Presenter {
    public UploadPresenter(UploadContract.View view) {
        super(new UploadModel(false), view);
    }

    @Override
    public void uploadImg(String chatlinkid, String filePath) {
        UploadImgReq req = new UploadImgReq(chatlinkid, filePath);
        req.setCancelTag(this);
        TioHttpClient.upload(req, new TioCallback<String>() {
            @Override
            public void onStart(Request<BaseResp<String>, ? extends Request> request) {
                super.onStart(request);
                TioLogger.i("正在上传中...");
                showUploadDialog();
            }

            @Override
            public void uploadProgress(Progress progress) {
                super.uploadProgress(progress);
                TioLogger.i("uploadProgress: " + progress);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                hideUploadDialog();
            }

            @Override
            public void onTioSuccess(String s) {
                TioLogger.i("上传完成: " + s);
            }

            @Override
            public void onTioError(String msg) {
                TioLogger.i("上传出错: " + msg);
                TioToast.showShort(msg);
            }
        });
    }

    @Override
    public void uploadVideo(String chatlinkid, String filePath) {
        UploadVideoReq req = new UploadVideoReq(chatlinkid, filePath);
        req.setCancelTag(this);
        TioHttpClient.upload(req, new TioCallback<String>() {
            @Override
            public void onStart(Request<BaseResp<String>, ? extends Request> request) {
                super.onStart(request);
                TioLogger.i("正在上传中...");
                showUploadDialog();
            }

            @Override
            public void uploadProgress(Progress progress) {
                super.uploadProgress(progress);
                TioLogger.i("uploadProgress: " + progress);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                hideUploadDialog();
            }

            @Override
            public void onTioSuccess(String s) {
                TioLogger.i("上传完成: " + s);
            }

            @Override
            public void onTioError(String msg) {
                TioLogger.i("上传出错: " + msg);
                TioToast.showShort(msg);
            }
        });
    }

    @Override
    public void uploadFile(String chatlinkid, String filePath) {
        UploadFileReq req = new UploadFileReq(chatlinkid, filePath);
        req.setCancelTag(this);
        TioHttpClient.upload(req, new TioCallback<String>() {
            @Override
            public void onStart(Request<BaseResp<String>, ? extends Request> request) {
                super.onStart(request);
                TioLogger.i("正在上传中...");
                showUploadDialog();
            }

            @Override
            public void uploadProgress(Progress progress) {
                super.uploadProgress(progress);
                TioLogger.i("uploadProgress: " + progress);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                hideUploadDialog();
            }

            @Override
            public void onTioSuccess(String s) {
                TioLogger.i("上传完成: " + s);
            }

            @Override
            public void onTioError(String msg) {
                TioLogger.i("上传出错: " + msg);
                TioToast.showShort(msg);
            }
        });
    }

    private void showUploadDialog() {
        Activity activity = getView().getActivity();
        if (activity != null) {
            SingletonProgressDialog.show_unCancel(activity, "上传中...");
        }
    }

    private void hideUploadDialog() {
        SingletonProgressDialog.dismiss();
    }

}
